package com.springboot.demo.design.pattern.instruction.user;

import com.alibaba.fastjson.JSONObject;
import com.springboot.common.entity.response.Result;
import com.springboot.db.entity.User;
import com.springboot.demo.dao.UserDao;
import com.springboot.demo.design.pattern.form.Form;
import com.springboot.demo.design.pattern.instruction.AbstractInstruction;
import com.springboot.demo.design.pattern.request.user.UserSaveParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;

/**
 * 用户保存指令器
 *
 * @author supu
 * @date 2019-03-19 16:30
 **/
@Log4j2
public class UserSaveInstruction extends AbstractInstruction<User, UserSaveParam> {
    protected final UserDao userDao;

    public UserSaveInstruction(Form form, UserDao userDao) {
        super(form);
        this.userDao = userDao;
    }

    @Override
    public void execute() {
        // 具体的业务逻辑执行
        UserSaveParam userSaveParam = form.getParameter().getData();
        log.info("保存用户请求参数：{}", JSONObject.toJSONString(userSaveParam));
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        userDao.saveUser(user);

        System.out.println(1/0);

        form.setResult(Result.newSuccess(user));
    }
}
